package com.aw.guiguzi.module.manager._user

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.constants.Extras
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwFormatCheckUtils
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.databinding.ActivityManagerResetPwdBinding
import com.aw.guiguzi.module.manager._user.presenters.ResetPwdPresent
import com.aw.guiguzi.module.manager._user.views.ResetPwdView

class UserResetPwdActivity :
    AwMvpBindingActivity<ResetPwdPresent?, ActivityManagerResetPwdBinding?>(), ResetPwdView.View {
    private var isOpenEyeNew = false
    private var isOpenEyeNewRe = false
    private var mBean: UserInfoBean? = null
    override fun createPresenter(): ResetPwdPresent {
        return ResetPwdPresent(this)
    }

    override fun getViewBinding(): ActivityManagerResetPwdBinding {
        return ActivityManagerResetPwdBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        setToolbar("重置密码", null)
    }

    override fun initData() {
        super.initData()
        binding?.etPwdNew!!.transformationMethod = PasswordTransformationMethod.getInstance()
        binding?.etPwdNewRe!!.transformationMethod = PasswordTransformationMethod.getInstance()
        mBean = intent.getSerializableExtra(Extras.params) as UserInfoBean?
        if (mBean == null) {
            showDialogToFinish("数据异常")
        }
    }

    override fun initListener() {
        super.initListener()
        binding?.ivEyeNew!!.setOnClickListener { v: View? ->
            if (isOpenEyeNew) {
                binding?.ivEyeNew!!.setImageResource(R.drawable.icon_eye_off)
                isOpenEyeNew = false
                binding?.etPwdNew!!.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding?.ivEyeNew!!.setImageResource(R.drawable.icon_eye_on)
                isOpenEyeNew = true
                binding?.etPwdNew!!.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
            binding?.etPwdNew!!.setSelection(getText(binding?.etPwdNew).length)
        }
        binding?.ivEyeNewRe!!.setOnClickListener { v: View? ->
            if (isOpenEyeNewRe) {
                binding?.ivEyeNewRe!!.setImageResource(R.drawable.icon_eye_off)
                isOpenEyeNewRe = false
                binding?.etPwdNewRe!!.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding?.ivEyeNewRe!!.setImageResource(R.drawable.icon_eye_on)
                isOpenEyeNewRe = true
                binding?.etPwdNewRe!!.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
            binding?.etPwdNewRe!!.setSelection(getText(binding?.etPwdNewRe).length)
        }
        findViewById<View>(R.id.tv_confirm).setOnClickListener { v: View? ->
            if (AwDataUtil.isEmpty(getText(binding?.etPwdNew))) {
                showMsg("请输入新密码")
                return@setOnClickListener
            }
            if (AwDataUtil.isEmpty(getText(binding?.etPwdNewRe))) {
                showMsg("请再次输入新密码")
                return@setOnClickListener
            }
            if (getText(binding?.etPwdNew).length < 6) {
                showMsg("密码长度不可小于6位")
                return@setOnClickListener
            }
            if (!AwFormatCheckUtils.isLetterDigitAll(getText(binding?.etPwdNew))) {
                showMsg("密码需包含字母与数字")
                return@setOnClickListener
            }
            if (getText(binding?.etPwdNew) != getText(binding?.etPwdNewRe)) {
                showMsg("两次密码输入不一致")
                return@setOnClickListener
            }
            mPresenter!!.resetPwd(mBean!!.id, getText(binding?.etPwdNew))
        }
    }

    override fun resetPwdSuccess() {
        showMsg("重置密码成功")
        finish()
    }
}